﻿using UnityEngine;
using System.Collections;
using System;

namespace iTManager
{
    public class iTweenEvent
    {
        private GameObject gameObject;
        private Hashtable hastable;
        private Action<GameObject, Hashtable> itweenActionWithGameObject;
        private Action<Hashtable> itweenActionWithoutGameObject;

        public Action OnStartEvent
        {
            set
            {
                hastable.Add("onstart", value);
            }
        }

        public Action<GameObject> OnStartWithArgEvent
        {
            set
            {
                hastable.Add("onstart", value);
                hastable.Add("onstartparams", gameObject);
            }
        }

        public Action OnUpdateEvent
        {
            set
            {
                hastable.Add("onupdate", value);
            }
        }

        /// <summary>
        /// use only with ValueTo
        /// </summary>
        public Action<float> OnUpdateValueToEvent
        {
            set
            {
                hastable.Add("onupdate", value);
            }
        }

        public Action<GameObject> OnUpdateWithArgEvent
        {
            set
            {
                hastable.Add("onupdate", value);
                hastable.Add("onupdateparams", gameObject);
            }
        }

        public Action OnCompleteEvent
        {
            set
            {
                hastable.Add("oncomplete", value);
            }
        }

        public Action<GameObject> OnCompleteWithArgEvent
        {
            set
            {
                hastable.Add("oncomplete", value);
                hastable.Add("oncompleteparams", gameObject);
            }
        }

        public iTweenEvent(Hashtable hastable, Action<Hashtable> itweenAction)
        {
            this.hastable = hastable;
            this.itweenActionWithoutGameObject = itweenAction;
        }

        public iTweenEvent(GameObject gameObject, Hashtable hastable, Action<GameObject, Hashtable> itweenAction)
        {
            this.hastable = hastable;
            this.gameObject = gameObject;
            this.itweenActionWithGameObject = itweenAction;
        }

        public void Start()
        {
            if(itweenActionWithGameObject != null)
                itweenActionWithGameObject.Invoke(gameObject, hastable);
            if (itweenActionWithoutGameObject != null)
                itweenActionWithoutGameObject.Invoke(hastable);
        }

        public iTweenEvent OnStart(Action action)
        {
            hastable.Add("onstart", action);
            return this;
        }

        public iTweenEvent OnStart(string methodName)
        {
            return OnStart(methodName, null, null);
        }

        public iTweenEvent OnStart(string methodName, GameObject onStartTarget)
        {
            return OnStart(methodName, onStartTarget, null);
        }

        public iTweenEvent OnStart(string methodName, GameObject onStartTarget, object arg)
        {
            hastable.Add("onstart", methodName);
            if (onStartTarget != null) hastable.Add("onstarttarget", onStartTarget);
            if (arg != null) hastable.Add("onstartparams", arg);
            return this;
        }

        public iTweenEvent OnUpdate(Action action)
        {
            hastable.Add("onupdate", action);
            return this;
        }

        /// <summary>
        /// use only with ValueTo
        /// </summary>
        public iTweenEvent OnUpdate(Action<float> action)
        {
            hastable.Add("onupdate", action);
            return this;
        }

        public iTweenEvent OnUpdate(string methodName)
        {
            return OnUpdate(methodName, null, null);
        }

        public iTweenEvent OnUpdate(string methodName, GameObject onUpdateTarget)
        {
            return OnUpdate(methodName, onUpdateTarget, null);
        }

        public iTweenEvent OnUpdate(string methodName, GameObject onUpdateTarget, object arg)
        {
            hastable.Add("onupdate", methodName);
            if (onUpdateTarget != null) hastable.Add("onupdatetarget", onUpdateTarget);
            if (arg != null) hastable.Add("onupdateparams", arg);
            return this;
        }

        public iTweenEvent OnComplete(Action action)
        {
            hastable.Add("oncomplete", action);
            return this;
        }

        public iTweenEvent OnComplete(string methodName)
        {
            return OnComplete(methodName, null, null);
        }

        public iTweenEvent OnComplete(string methodName, GameObject onCompletedTarget)
        {
            return OnComplete(methodName, onCompletedTarget, null);
        }

        public iTweenEvent OnComplete(string methodName, GameObject onCompletedTarget, object arg)
        {
            hastable.Add("oncomplete", methodName);
            if (onCompletedTarget != null) hastable.Add("oncompletetarget", onCompletedTarget);
            if (arg != null) hastable.Add("oncompleteparams", arg);
            return this;
        }

        public void Stop()
        {
            iTweenBase.Stop(gameObject);
        }

        public void Stop(string type)
        {
            iTweenBase.Stop(gameObject, type);
        }

        public void Stop(bool includeChildren)
        {
            iTweenBase.Stop(gameObject, includeChildren);
        }

        public void Stop(string type, bool includeChildren)
        {
            iTweenBase.Stop(gameObject, type, includeChildren);
        }

        public void Pause()
        {
            iTweenBase.Pause(gameObject);
        }

        public void Pause(string type)
        {
            iTweenBase.Pause(gameObject, type);
        }

        public void Pause(bool includeChildren)
        {
            iTweenBase.Pause(gameObject, includeChildren);
        }

        public void Pause(string type, bool includeChildren)
        {
            iTweenBase.Pause(gameObject, type, includeChildren);
        }
    }
}
